package com.example.pjy.controller;

import com.example.pjy.pojo.Academic;
import com.example.pjy.pojo.Result;
import com.example.pjy.pojo.Student;
import com.example.pjy.service.AcademicService;
import com.example.pjy.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/academic")
public class AcademicController {

    @Resource
    private AcademicService academicService;







    //把接收到的json转为实体类对象 运行时给的是json
    @PostMapping("/add")
    public Result add(@RequestBody Academic academic) {
        if(academicService.isExist(academic.getStudentId())){
            return Result.error("该学号已存在");
        }
        academicService.add(academic);
        return Result.success();
    }


    //查询 有主体数据给浏览器响应 主题数据是数组 数组里有多个对象
    @GetMapping("/selectlist")
    public Result<List<Academic>> list(){
        List<Academic> academic= academicService.list();
        return Result.success(academic);//结果是数组
    }


    //根据id找学生
    @GetMapping("/detail")
    public Result<Academic> detail(Integer studentId){//student响应给前端浏览器是json  id接收前端传来的id
        Academic academic = academicService.findById(studentId);
        return Result.success(academic);//结果是对象
    }


    //把json转为对象
    @PutMapping("/updateac")
    public Result updateac(@RequestBody Academic academic){
        academicService.updateac(academic);
        return Result.success();
    }


    @DeleteMapping("/delete/{studentId}")
    public Result delete(@PathVariable("studentId") Integer studentId){
        if (studentId == null) {
            return Result.error("学生ID不能为空");
        }
        academicService.delete(studentId);
        return Result.success("删除成功");
    }
}
